package com.nstar.orderexpress.view
{
	import com.nstar.base.controls.AnimatedTileList;
	import com.nstar.base.util.EffectUtils;
	import com.nstar.orderexpress.control.OrderMenuThumbEvent;
	import com.nstar.orderexpress.model.Model;
	import com.nstar.orderexpress.vo.OrderMenuVO;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.events.FlexEvent;
	
	public class MainViewClass extends VBox
	{
		public var atlRests:AnimatedTileList;
		public var atlMenus:AnimatedTileList;
		
		private var model:Model = Model.getInstance();
		
		public function MainViewClass()
		{
			//TODO: implement function
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			initSampleData();
			atlMenus.addEventListener(OrderMenuThumbEvent.PURCHASE,orderMenuThumbEventHandler);
			atlMenus.addEventListener(OrderMenuThumbEvent.ADD_FAVORITE,orderMenuThumbEventHandler);			
		}
		
		private function initSampleData():void{
			var ary:ArrayCollection = new ArrayCollection();
			for(var i:int = 0;i<10;i++){
				var obj:Object = new Object();
				obj.name = "nanjing hotel "+i;
				obj.address = "nanjing zhongshan road "+i+" #";
				ary.addItem(obj);
			}
			atlRests.dataProvider = ary;
			
			ary.removeAll();
			for(var i:int = 0;i<10;i++){
				var omv:OrderMenuVO = new OrderMenuVO();
				omv.menuId = i.toString();
				omv.name = "menu "+i;
				omv.price = 50*Math.random();
				omv.menuDesc = "this is "+omv.name;
				ary.addItem(omv);
			}
			atlMenus.dataProvider = ary;
		}
		
		//添加到购物车、收藏夹
		private function orderMenuThumbEventHandler(event:OrderMenuThumbEvent):void{
			switch (event.type){
				case OrderMenuThumbEvent.PURCHASE:
					model.mainApp.shoppingCartView.omlSelected.addOrderMenu(event.orderMenu);
					model.mainApp.shoppingCartView.dispatchEvent(event);
					break;
				case OrderMenuThumbEvent.ADD_FAVORITE:
					model.mainApp.favoriteView.omlFavorites.addOrderMenu(event.orderMenu);
					model.mainApp.favoriteView.dispatchEvent(event);
					break;
			}
			EffectUtils.moveAndDisppear(event.source, 20, 200);			
			
		}		

	}
}